import $ from 'jquery'
import { Loading } from 'element-ui';


var hexcase = 0;
var chrsz = 8;

function safe_add(x, y) {
  var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  return (msw << 16) | (lsw & 0xFFFF);
}

/*
 * Bitwise rotate a 32-bit number to the left.
 */
function bit_rol(num, cnt) {
  return (num << cnt) | (num >>> (32 - cnt));
}

function md5_cmn(q, a, b, x, s, t) {
  return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
}

function md5_ff(a, b, c, d, x, s, t) {
  return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}

function md5_gg(a, b, c, d, x, s, t) {
  return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}

function md5_hh(a, b, c, d, x, s, t) {
  return md5_cmn(b ^ c ^ d, a, b, x, s, t);
}

function md5_ii(a, b, c, d, x, s, t) {
  return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
}

function binl2hex(binarray) {
  var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  var str = "";
  for (var i = 0; i < binarray.length * 4; i++) {
    str += hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8 + 4)) & 0xF) +
      hex_tab.charAt((binarray[i >> 2] >> ((i % 4) * 8)) & 0xF);
  }
  return str;
}

function str2binl(str) {
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for (var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (i % 32);
  return bin;
}

function core_md5(x, len) {
  /* append padding */
  x[len >> 5] |= 0x80 << ((len) % 32);
  x[(((len + 64) >>> 9) << 4) + 14] = len;

  var a = 1732584193;
  var b = -271733879;
  var c = -1732584194;
  var d = 271733878;

  for (var i = 0; i < x.length; i += 16) {
    var olda = a;
    var oldb = b;
    var oldc = c;
    var oldd = d;

    a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
    d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
    c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
    b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
    a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
    d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
    c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
    b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
    a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
    d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
    c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
    b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
    a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
    d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
    c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
    b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);

    a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
    d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
    c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
    b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
    a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
    d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
    c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
    b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
    a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
    d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
    c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
    b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
    a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
    d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
    c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
    b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);

    a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
    d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
    c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
    b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
    a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
    d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
    c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
    b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
    a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
    d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
    c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
    b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
    a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
    d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
    c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
    b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);

    a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
    d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
    c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
    b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
    a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
    d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
    c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
    b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
    a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
    d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
    c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
    b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
    a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
    d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
    c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
    b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);

    a = safe_add(a, olda);
    b = safe_add(b, oldb);
    c = safe_add(c, oldc);
    d = safe_add(d, oldd);
  }
  return Array(a, b, c, d);

}

(function () {

  let t = {};
  t.baseUrl = 'http://13.125.226.45/BilurGame/'
  // t.baseUrl = 'http://192.168.20.122:8080/BilurGame/'
  t.jiayibaseUrl = 'http://52.79.213.0/'
  t.url = {
    login: t.baseUrl + 'login/login', //登录
    ginseng: t.baseUrl + 'game/jsonToGinseng', //人参
    chemyList: t.baseUrl + 'game/jsonToAlecemy', //炼丹
    examine: t.baseUrl + 'recharge/updateRechargeStatus?id=22&isPass=1', //审核
    tibi: t.jiayibaseUrl + 'tokenRecord/tokenRecord/saveBigAdminLogRechargeFromGame', //交易所提币
    exchangeConsume: t.baseUrl + 'exchange/consume',
    add: t.baseUrl + 'recharge/addRecharge', //充提币
    updateStatus: t.baseUrl + 'recharge/updateRechargeStatus',
    updateRecord: t.baseUrl + 'RechargeRecord/updateRechargeRecord',
    usreALL: t.jiayibaseUrl + 'appUser/appuser/alluserinfo', //用户列表
    sell: t.jiayibaseUrl + 'tokenRecord/tokenRecord/saveThridBigAppChargeProvided', //售出
    register: t.baseUrl + 'login/register', //注册
    verifyCode: t.baseUrl + 'login/getCheckCode', //获取验证码
    phoneCheck: t.baseUrl + 'exchange/checkUserIsExist', //手机号码验证
    forget: t.baseUrl + 'login/forgetPassword', //忘记密码
    checked: t.baseUrl + 'recharge/findUserInfo', //查询
    exchangeRate: t.baseUrl + '/winning/findAll', //查询游戏所有汇率信息
    sellRorJ: t.baseUrl + 'rechargeSell/addRechargeSell', // 卖出人参或者金丹
    exchangeRecharge: t.baseUrl + 'exchange/rechange',
    updateSell: t.baseUrl + 'rechargeSell/updateRechargeSell',
    addBpx: t.baseUrl + 'RechargeRecord/addRechargeRecord', //添加BPX充值记录
    // consume: t.baseUrl + 'gameRecord/addGameRecord', //消耗
    consume: t.baseUrl + 'activity/playAlchemy',
    sellList: t.baseUrl + 'rechargeSell/findAlchemySellReCordList', //炼丹的售出记录
    sellListGinseng: t.baseUrl + 'rechargeSell/findGinsengSellReCordList', //人参的售出记录
    expenditure: t.baseUrl + 'balanceRecord/findBalanceRecordByPhone', //收支记录
    operation: t.baseUrl + 'operationLog/findOperationLogByPhone', //操作记录
    winningResult: t.baseUrl + 'activity/joinActivityList', //查看用户是否中奖
    Coupons: t.baseUrl + 'activity/getTicketList', //查询当前人参与的活动的票券
    findActivityStatus: t.baseUrl + 'activity/findActivityStatus',//根据游戏类型查看当前活动的状态
    playGame: t.baseUrl + 'activity/playGame',//玩游戏接口
    findActivityPercent: t.baseUrl + 'activity/findActivityPercent',//查询当前游戏的投入百分比
    getLdResult: t.baseUrl + 'activity/getPlayAlchemy',//
    isPlay: t.baseUrl + 'activity/findGameStatus',//
    coinType: t.baseUrl + 'coinType/findCoinType',// 游戏区列表
    coinTypetax: t.baseUrl + "coinType/findCoinTypeById",//游戏区汇率
    seednotice: t.baseUrl + 'activity/findNoticListforGinsing',//挖参公告
    indexnotice: t.baseUrl + 'activity/findNoticListforDY',//挖参公告
    progress: t.baseUrl + 'activity/findAllActivityInfo',//挖参首页三个进度条
  }
  t.ajax = (obj, callback) => {

    if (callback && typeof callback == 'function') {
      let loadingInstance = Loading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.7)',
      });
      $.ajax({
        url: obj.url, //可以不是本地域名
        type: obj.type,
        dataType: obj.json || 'jsonp', //jsonp格式访问
        // jsonp:"callback",
        jsonpCallback: 'jsonpCallback', //获取数据的函数
        data: obj.dataJsonp || JSON.stringify(obj.data) || '',
        contentType: obj.contentType || 'application/x-www-form-urlencoded;charset=UTF-8',
        success: function (data) {
          setTimeout(() => {
            loadingInstance.close();
          }, 1000)
          if (data) {
            callback(data)
          }
        },
        error(error) {
          setTimeout(() => {
            loadingInstance.close();
          }, 1000)
        }
      })
    } else {
      console.log('请输入正确的回调类型')
    }
  }
  t.indexnotice = (callback) => {
    t.ajax({
      url: t.url.indexnotice + '?cointype=' + localStorage.getItem('cointype'),
      type: 'get',
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.seednotice = (callback) => {
    t.ajax({
      url: t.url.seednotice + '?cointype=' + localStorage.getItem('cointype'),
      type: 'get',
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.expenditure = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.expenditure,
      dataJsonp: data,
      type: 'post',
      contentType: 'application/json;charset=UTF-8'
    }, callback)
  },
    t.operation = (data, callback) => {
      data.cointype = localStorage.getItem('cointype')
      t.ajax({
        url: t.url.operation,
        dataJsonp: data,
        type: 'post',
        contentType: 'application/json;charset=UTF-8'
      }, callback)
    }
  t.findActivityStatus = (data, callback) => {
    t.ajax({
      url: t.url.findActivityStatus + '?type=' + data.type + '&cointype=' + localStorage.getItem('cointype'),
      dataJsonp: data,
      type: 'get',
      contentType: 'application/json;charset=UTF-8'
    }, callback)
  }
  t.playGame = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.playGame,
      dataJsonp: data,
      type: 'post',
      contentType: 'application/json;charset=UTF-8'
    }, callback)
  }
  t.findActivityPercent = (data, callback) => {
    t.ajax({
      url: t.url.findActivityPercent + '?activityId=' + data.activityId + '&cointype=' + localStorage.getItem('cointype'),
      // dataJsonp: data,
      json: 'jsonp',
      type: 'post',
      contentType: 'application/json;charset=UTF-8'
    }, callback)
  }
  t.login = (data, callback) => {
    t.ajax({
      url: t.url.login,
      type: 'post',
      dataJsonp: data,
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.register = (data, callback) => {
    t.ajax({
      url: t.url.register,
      type: 'get',
      dataJsonp: data,
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.verifyCode = (data, callback) => {
    t.ajax({
      url: t.url.verifyCode,
      type: 'get',
      dataJsonp: data,
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.forget = (data, callback) => {
    t.ajax({
      url: t.url.forget,
      type: 'post',
      dataJsonp: data,
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.tibi = (data, callback) => {
    t.ajax({
      url: t.url.tibi,
      type: 'post',
      data: data,
      json: 'json',
      contentType: 'application/json;charset=UTF-8'
    }, callback)
  }
  t.phoneCheck = (data, callback) => {
    t.ajax({
      url: t.url.phoneCheck,
      type: 'get',
      dataJsonp: data,
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.sell = (data, callback) => {
    t.ajax({
      url: t.url.sell,
      type: 'post',
      data: data,
      json: 'json',
      contentType: 'application/json;charset=UTF-8'
    }, callback)
  }
  t.sellList = (data, callback) => {
    t.ajax({
      url: t.url.sellList,
      dataJsonp: data,
      type: 'post',
      contentType: 'application/json;charset=UTF-8'
    }, callback)
  }
  t.sellListGinseng = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.sellListGinseng,
      dataJsonp: data,
      type: 'post',
      contentType: 'application/json;charset=UTF-8'
    }, callback)
  }
  t.progress = (data, callback) => {
    t.ajax({
      url: t.url.progress + '?cointype=' + data.cointype ,
      type: 'get',
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.usreALL = (callback) => {
    t.ajax({
      url: t.url.usreALL,
      type: 'post',
      json: 'json'
    }, callback)
  }
  t.ginseng = (callback) => {
    t.ajax({
      url: t.url.ginseng,
      type: 'get'
    }, callback)
  }
  t.chemyList = (callback) => {
    t.ajax({
      url: t.url.chemyList,
      type: 'get'
    }, callback)
  }
  t.examine = (callback) => {
    t.ajax({
      url: t.url.examine,
      type: 'get'
    }, callback)
  }
  t.checked = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.checked,
      type: 'post',
      dataJsonp: data,
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.exchangeRate = (callback) => {
    t.ajax({
      url: t.url.exchangeRate,
      type: 'post',
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.exchangeRecharge = (data, callback) => {
    t.ajax({
      url: t.url.exchangeRecharge,
      type: 'get',
      dataJsonp: data,
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.sellRorJ = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.sellRorJ,
      dataJsonp: data,
      type: 'post'
    }, callback)
  }
  t.updateSell = (data, callback) => {
    t.ajax({
      url: t.url.updateSell,
      dataJsonp: data,
      type: 'get'
    }, callback)
  }
  t.addBpx = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.addBpx,
      dataJsonp: data,
      type: 'post'
    }, callback)
  }
  t.exchangeConsume = (data, callback) => {
    t.ajax({
      url: t.url.exchangeConsume,
      type: 'get',
      dataJsonp: data,
      contentType: 'application/json;charset=UTF-8',
      json: 'jsonp',
    }, callback)
  }
  t.add = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.add,
      dataJsonp: data,
      type: 'post'
    }, callback)
  }
  t.updateStatus = (data, callback) => {
    t.ajax({
      url: t.url.updateStatus,
      dataJsonp: data,
      type: 'get'
    }, callback)
  }
  t.updateRecord = (data, callback) => {
    t.ajax({
      url: t.url.updateRecord,
      dataJsonp: data,
      type: 'get'
    }, callback)
  }
  t.consume = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.consume,
      dataJsonp: data,
      type: 'post'
    }, callback)
  }
  t.isPlay = (callback) => {
    t.ajax({
      url: t.url.isPlay,
      type: 'get'
    }, callback)
  }
  t.getLdResult = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.getLdResult,
      dataJsonp: data,
      type: 'post'
    }, callback)
  }
  t.winningResult = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.winningResult,
      dataJsonp: data,
      type: 'get',
    }, callback)
  }
  t.Coupons = (data, callback) => {
    data.cointype = localStorage.getItem('cointype')
    t.ajax({
      url: t.url.Coupons,
      dataJsonp: data,
      type: 'get'
    }, callback)
  }
  t.coinType = (callback) => {
    t.ajax({
      url: t.url.coinType,
      type: 'get',
    }, callback)
  }
  t.coinTypetax = (data, callback) => {
    t.ajax({
      url: t.url.coinTypetax,
      dataJsonp: data,
      type: 'post',
    }, callback)
  }
  t.setStore = (key, obj) => {
    localStorage.setItem(key, JSON.stringify(obj))
  }
  t.getStore = (key) => {
    return JSON.parse(localStorage.getItem(key))
  }
  t.removeStore = (key) => {
    localStorage.removeItem(key)
  }
  t.hex_md5 = function (s) {
    return binl2hex(core_md5(str2binl(s), s.length * chrsz));
  }
  t.tirm = (str) => {
    return str.replace(/^\s+|\s+$/g, '')
  }
  t.userInfo = (callback) => {
    t.flag = false
    if (t.getStore("user") && t.getStore("user").userid && t.getStore('winning')) {
      t.checked({ phone: t.getStore("user").userid }, data => {
        if (data && data[0] && data[0].user) {
          t.setStore('userInfo', data[0].user);
          t.setStore(
            "mount",
            data[0].user.seeds_amount * t.getStore('winning').gold +
            data[0].user.silver_seeds_amount * t.getStore('winning').silver +
            data[0].user.copper_seeds_amount * t.getStore('winning').copper
          );
          if(!callback){
            t.flag = true
          }
          callback && callback()
        }
      });
    }
  }
  window.u = t
})()

u.userInfo()
export default u
